package controller

import (
	"RocFamilySystem/internal/service"
	"RocFamilySystem/models"
	"RocFamilySystem/pkg/constant"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/url"
	"strconv"
	"strings"
)

type WorkTravelController struct {
	BaseController
	WorkTravelService *service.WorkTravelService
}

// 初始化
func init() {
	registerContainer["WorkTravelController"] = &WorkTravelController{}
}

// SetService 设置服务
func (controller *WorkTravelController) SetService(workTravelService *service.WorkTravelService) {
	controller.WorkTravelService = workTravelService
}

// RegisterRouter 注册路由
func (controller *WorkTravelController) RegisterRouter(engine *gin.Engine) {
	prefix := "/workTravel"
	g := engine.Group(prefix)
	prefixPermission := "permission" + strings.ReplaceAll(prefix, "/", ":")
	{
		//新增工作出差
		g.POST("", controller.AddWorkTravel)
		registerPermissionContainer("新增工作出差", prefixPermission+":create", prefix, "POST")
		//删除工作出差
		g.DELETE("/:id", controller.RemoveWorkTravel)
		registerPermissionContainer("删除工作出差", prefixPermission+":delete", prefix, "DELETE")
		//修改工作出差
		g.PUT("", controller.ModifyWorkTravel)
		registerPermissionContainer("修改工作出差", prefixPermission+":update", prefix, "PUT")
		//获取工作出差列表
		g.POST("/list", controller.QueryWorkTravel)
		registerPermissionContainer("查询工作出差列表", prefixPermission+":list", prefix, "POST")
		//通过ID查询工作出差
		g.GET("/:id", controller.QueryWorkTravelById)
		registerPermissionContainer("ID查询工作出差", prefixPermission+":id", prefix, "GET")

		//批量删除工作出差
		g.DELETE("/batchDelete", controller.RemoveWorkTravelBatch)
		registerPermissionContainer("批量删除工作出差", prefixPermission+":batchDelete", prefix, "DELETE")
		//批量导入模板下载
		g.GET("/templateDownload", controller.WorkTravelBatchImportTemplateDownload)
		registerPermissionContainer("工作出差批量导入模板下载", prefixPermission+":templateDownload", prefix, "GET")
		//批量导入
		g.GET("/import", controller.WorkTravelBatchImportTemplateDownload)
		registerPermissionContainer("工作出差批量导入", prefixPermission+":import", prefix, "POST")
		//批量导出
		g.GET("/export", controller.WorkTravelBatchExport)
		registerPermissionContainer("工作出差批量导出", prefixPermission+":export", prefix, "POST")

	}
}

// AddWorkTravel 添加工作出差
// @Summary AddWorkTravel 添加工作出差
// @Tags 工作出差
// @Produce json
// @Param workTravel body models.WorkTravel true "工作出差"
// @Success 200 {object} Result "成功"
// @Router /workTravel [post]
func (controller *WorkTravelController) AddWorkTravel(ctx *gin.Context) {
	var workTravel models.WorkTravel
	err := ctx.ShouldBind(&workTravel)
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.WorkTravelService.AddWorkTravel(&workTravel, controller.GetLoginUserId(ctx))
	controller.CustomResult(ctx, 200, "添加工作出差成功", nil)
}

// RemoveWorkTravel 删除工作出差
// @Summary RemoveWorkTravel  删除工作出差
// @Tags 工作出差
// @Produce json
// @Param id path int true "工作出差id"
// @Success 200 {object} Result "成功"
// @Router /workTravel/:id [delete]
func (controller *WorkTravelController) RemoveWorkTravel(ctx *gin.Context) {
	workTravelId, err := strconv.Atoi(ctx.Param("id"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.WorkTravelService.RemoveWorkTravel(workTravelId, controller.GetLoginUserId(ctx))
	controller.CustomResult(ctx, 200, fmt.Sprintf("删除【%d】成功", workTravelId), nil)
}

// ModifyWorkTravel 修改工作出差
// @Summary ModifyWorkTravel 修改工作出差
// @Tags 工作出差
// @Produce json
// @Param workTravel body models.WorkTravel true "工作出差"
// @Success 200 {object} Result "成功"
// @Router /workTravel [put]
func (controller *WorkTravelController) ModifyWorkTravel(ctx *gin.Context) {
	var workTravel models.WorkTravel
	err := ctx.ShouldBind(&workTravel)
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.WorkTravelService.ModifyWorkTravel(&workTravel, controller.GetLoginUserId(ctx))
	controller.CustomResult(ctx, 200, fmt.Sprintf("修改【%d】成功", *workTravel.Id), nil)
}

// QueryWorkTravel 查询工作出差列表
// @Summary QueryWorkTravel 查询工作出差列表
// @Tags 工作出差
// @Produce json
// @Param workTravel body models.WorkTravel true "工作出差"
// @Success 200 {object} Result "成功"
// @Router /workTravel/list [post]
func (controller *WorkTravelController) QueryWorkTravel(ctx *gin.Context) {
	pageNum, pageSize := controller.GetPageInfo(ctx)
	var workTravel models.WorkTravel
	err := ctx.ShouldBind(&workTravel)
	controller.HandleError(err, constant.PARAMS_ERROR)
	data := controller.WorkTravelService.QueryWorkTravel(&workTravel, pageNum, pageSize, controller.GetLoginUserId(ctx))
	controller.SuccessResult(ctx, data)
}

// QueryWorkTravelById 查询工作出差通过ID
// @Summary QueryWorkTravelById  查询工作出差通过ID
// @Tags 工作出差
// @Produce json
// @Param id path int true "工作出差id"
// @Success 200 {object} Result "成功"
// @Router /workTravel/:id [get]
func (controller *WorkTravelController) QueryWorkTravelById(ctx *gin.Context) {
	workTravelId, err := strconv.Atoi(ctx.Param("id"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	data := controller.WorkTravelService.QueryWorkTravelById(workTravelId, controller.GetLoginUserId(ctx))
	controller.SuccessResult(ctx, data)
}

// RemoveWorkTravelBatch 批量删除工作出差
// @Summary RemoveWorkTravel  批量删除工作出差
// @Tags 工作出差
// @Produce json
// @Param ids[] query []int true "工作出差id"
// @Success 200 {object} Result "成功"
// @Router /workTravel/batchDelete [delete]
func (controller *WorkTravelController) RemoveWorkTravelBatch(ctx *gin.Context) {
	ids := ctx.QueryArray("ids[]")
	if len(ids) == 0 {
		controller.HandleError(errors.New("参数缺失"), constant.PARAMS_ERROR)
	}
	controller.WorkTravelService.RemoveWorkTravelBatch(ids, controller.GetLoginUserId(ctx))
	controller.CustomResult(ctx, 200, fmt.Sprintf("删除【%v】成功", ids), nil)
}

// WorkTravelBatchImportTemplateDownload 批量导入模板下载
// @Summary WorkTravelBatchImportTemplateDownload 批量导入模板下载
// @Tags 工作出差
// @Produce json
// @Success 200 {object} Result "成功"
// @Router /workTravel/templateDownload [get]
func (controller *WorkTravelController) WorkTravelBatchImportTemplateDownload(ctx *gin.Context) {
	excelFile := controller.WorkTravelService.WorkTravelTemplateDownload()
	ctx.Writer.Header().Set("Content-Type", "application/octet-stream")
	ctx.Writer.Header().Set("Content-Disposition", "attachment;filename=工作出差.xlsx")
	excelFile.Write(ctx.Writer)
}

// WorkTravelBatchImport 批量导入
// @Summary WorkTravelBatchImport 批量导入
// @Tags 工作出差
// @Produce json
// @Success 200 {object} Result "成功"
// @Router /workTravel/import [post]
func (controller *WorkTravelController) WorkTravelBatchImport(ctx *gin.Context) {
	file, err := ctx.FormFile("file")
	if err != nil {
		controller.HandleError(err, constant.UPLOAD_FILE_ERROR)
	}
	controller.WorkTravelService.AddWorkTravelBatch(file)
	controller.CustomResult(ctx, 200, "批量导入成功", nil)
}

// WorkTravelBatchExport 批量导出
// @Summary WorkTravelBatchExport 批量导出
// @Tags 工作出差
// @Produce json
// @Param ids params []int true 导出ids
// @Success 200 {object} Result "成功"
// @Router /workTravel/export [post]
func (controller *WorkTravelController) WorkTravelBatchExport(ctx *gin.Context) {
	ids := ctx.QueryArray("ids[]")
	if len(ids) == 0 {
		controller.HandleError(errors.New("参数缺失"), constant.PARAMS_ERROR)
	}
	err := ctx.ShouldBind(&ids)
	controller.HandleError(err, constant.PARAMS_ERROR)
	excelFile := controller.WorkTravelService.WorkTravelExport(ids)
	ctx.Writer.Header().Set("Content-Type", "application/octet-stream")
	ctx.Writer.Header().Set("Content-Disposition", "attachment;filename="+url.QueryEscape("工作出差.xlsx"))
	excelFile.Write(ctx.Writer)
}
